<?php
add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );
/**
 * Init plugin options to white list our options
 */
function theme_options_init(){
	register_setting( 'petwares_options', 'petwares_theme_options', 'theme_options_validate' );
}
/**
 * Load up the menu page
 */
function theme_options_add_page() {
	add_theme_page( __( 'Theme Options', 'pet-wares' ), __( 'Theme Options', 'pet-wares' ), 'edit_theme_options', 'theme_options', 'theme_options_do_page' );
}
function theme_options_do_page() {
	global $select_options, $radio_options;
	if ( ! isset( $_REQUEST['settings-updated'] ) )
		$_REQUEST['settings-updated'] = false;
	?>
	<div class="wrap">
		<?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options', 'pet-wares' ) . "</h2>"; ?>
		<?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
		<div class="updated fade"><p><strong><?php _e( 'Options saved', 'pet-wares' ); ?></strong></p></div>
		<?php endif; ?>
		<form method="post" action="options.php">
			<?php settings_fields( 'petwares_options' ); ?>
			<?php $options = get_option( 'petwares_theme_options' ); ?>
			<table class="form-table">
				<?php
				/**
				 * A sample textarea option
				 */
				?>
				<tr valign="top"><th scope="row"><?php _e( 'Before Join Form', 'pet-wares' ); ?></th>
					<td>
						<textarea id="petwares_theme_options[beforeForm]" class="large-text" cols="50" rows="10" name="petwares_theme_options[beforeForm]"><?php echo esc_textarea( $options['beforeForm'] ); ?></textarea>					
					</td>
				</tr>	
				<tr valign="top"><th scope="row"><?php _e( 'Join Pet Wares Form Code', 'pet-wares' ); ?></th>
					<td>
						<textarea id="petwares_theme_options[formCode]" class="large-text" cols="50" rows="10" name="petwares_theme_options[formCode]"><?php echo esc_textarea( $options['formCode'] ); ?></textarea>					
					</td>
				</tr>
				<tr valign="top"><th scope="row"><?php _e( 'Instruction for Select Field', 'pet-wares' ); ?></th>
					<td>
						<textarea id="petwares_theme_options[select-text]" class="large-text" cols="20" rows="2" name="petwares_theme_options[select-text]"><?php echo esc_textarea( $options['select-text'] ); ?></textarea>					
					</td>
				</tr>
				<tr valign="top"><th scope="row"><?php _e( 'Category Dropdown ID', 'pet-wares' ); ?></th>
					<td>
						<input id="petwares_theme_options[category]" name="petwares_theme_options[category]" value='<?php echo _e( $options['category'] ); ?>' />				
					</td>
				</tr>			
				<tr valign="top"><th scope="row"><?php _e( 'Form Page ID', 'pet-wares' ); ?></th>
					<td>
						<input id="petwares_theme_options[form-page-id]" name="petwares_theme_options[form-page-id]" value='<?php echo _e( $options['form-page-id'] ); ?>' />			
					</td>
				</tr>
			</table>
			<p class="submit">
				<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'pet-wares' ); ?>" />
			</p>
		</form>
	</div>
	<?php
}
/**
 * Sanitize and validate input. Accepts an array, return a sanitized array.
 */
function theme_options_validate( $input ) {
	$html = $input['formCode'];
	$dom = new DOMDocument;                 // init new DOMDocument
	$dom->loadHTML($html);                  // load HTML into it
	$xpath = new DOMXPath($dom);            // create a new XPath
	$nodes = $xpath->query('//*[@style]');  // Find elements with a style attribute
	if(count($nodes) != 0) {
		foreach($nodes as $node) {              // Iterate over found elements
			$node->removeAttribute('style');    // Remove style attribute
		}
	}
	//adding size="40" to input field
	$nodes = $xpath->query('//input');
	if(count($nodes) != 0) {
		foreach($nodes as $node) {              
			$type = $node->getAttribute('type');
			if($type != 'hidden'){
				$node->setAttribute("size", "30");
			}
		}
	}	
	//styling textarea
	$nodes = $xpath->query('//textarea');
	if(count($nodes) != 0) {
		foreach($nodes as $node) {              
				$node->setAttribute("cols", "25");
				$node->setAttribute("rows", "5");
		}
	}
	// the table by its tag name 
	$tables = $dom->getElementsByTagName('table');
		//remove width attribute in table tag
		if($tables->length != 0){
			//removed width attribute
			$tables->item(0)->removeAttribute('width');
			//add ID to table id='form-table'
    		$tables->item(0)->setAttribute('id', 'form-table');
	   		$rows = $tables->item(0)->getElementsByTagName('tr');   
	   		$rowCount=0;
			if($rows->length != 0){
				$rows->item(0)->setAttribute("class", "form-required");
				$rows->item($rows->length-1)->setAttribute("class", "submit-reset");
				foreach ($rows as $row) {
					// get each column by tag name 
					$cols = $row->getElementsByTagName('td');
					// echo the values 
					$count=0;
					foreach( $cols as $col) {
						if($count==0){
							$value = $col->nodeValue;
							//adding class to first column
							$col->setAttribute("class", "input-field-col1");
							$col->removeAttribute("nowrap");
							$col->removeAttribute("align");
							if($value=="Web Form"){					
								$col->nodeValue = "* Required Fields";
								//$trToRemove = $tables->item(0)->getElementsByTagName('tr')->item(0);
   								//$oldtr = $tables->item(0)->addChild($trToRemove);
							}						
						}
						else{
							//adding class to second column
							$col->setAttribute("class", "input-field-col2");
						}
						$count++;
					}
					$rowCount++;
				}
				
			} 	
		}
	$input['formCode'] =  $dom->saveHTML();                  // output cleaned HTML
	//removing : from label and add *
	$input['formCode'] = preg_replace('/&nbsp;&nbsp;:/', '<span class="required">*</span>', $input['formCode']);
	
	//$input['formCode'] = $input['formCode'].$replaceValue;
	return $input;
}

function createElement($domObj, $tag_name, $value = NULL, $attributes = NULL)
{
    $element = ($value != NULL ) ? $domObj->createElement($tag_name, $value) : $domObj->createElement($tag_name);

    if( $attributes != NULL )
    {
        foreach ($attributes as $attr=>$val)
        {
            $element->setAttribute($attr, $val);
        }
    }

    return $element;
}
?>